package com.atzc.sink;

import com.atzc.bean.CarDetailLogBean;
import com.atzc.utils.mysql.MysqlConnect;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.functions.sink.RichSinkFunction;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;


public class CarDetailLogSinkMysql extends RichSinkFunction<CarDetailLogBean> {

    private PreparedStatement ps = null;
    private Connection connection = MysqlConnect.getConnection();

    @Override
    public void open(Configuration parameters) throws Exception {
        super.open(parameters);
        try {
            String sql = "insert into cardetail_log_new(create_time,requestid,carno,create_date,import_date,update_time,`schema`,scenecode,longrentcarflag,os,token,mac,endtime,starttime,getcarlat,getcarlon,getcarflag,queryid,caraddrindex,isfirst,citycode,osversion,appversion,imei,oaid,publiclongitude,publiclatitude,publiccitycode,appname,devicename,publictoken,appchannelid,androidid,memno,meminfocache,returncarlon,returncarlat,returncarflag,h5platform ) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
            ps = connection.prepareStatement(sql);
        }catch (SQLException e){
            System.out.println("error:车辆详情日志编译sql出错了");
        }

    }

    @Override
    public void invoke(CarDetailLogBean car_detail_log, Context context) throws Exception {
        //设置字段
        try {
            ps.setString(1,car_detail_log.getCreate_time());
            ps.setString(2,car_detail_log.getRequestid());
            ps.setString(3,car_detail_log.getCarno());
            ps.setString(4,car_detail_log.getCreate_date());
            ps.setString(5,car_detail_log.getImport_date());
            ps.setString(6,car_detail_log.getUpdate_time());
            ps.setString(7,car_detail_log.getSchema());
            ps.setString(8,car_detail_log.getScenecode());
            ps.setString(9,car_detail_log.getLongrentcarflag());
            ps.setString(10,car_detail_log.getOs());
            ps.setString(11,car_detail_log.getToken());
            ps.setString(12,car_detail_log.getMac());
            ps.setString(13,car_detail_log.getEndtime());
            ps.setString(14,car_detail_log.getStarttime());
            ps.setString(15,car_detail_log.getGetcarlat());
            ps.setString(16,car_detail_log.getGetcarlon());
            ps.setString(17,car_detail_log.getGetcarflag());
            ps.setString(18,car_detail_log.getQueryid());
            ps.setString(19,car_detail_log.getCaraddrindex());
            ps.setString(20,car_detail_log.getIsfirst());
            ps.setString(21,car_detail_log.getCitycode());
            ps.setString(22,car_detail_log.getOsversion());
            ps.setString(23,car_detail_log.getAppversion());
            ps.setString(24,car_detail_log.getImei());
            ps.setString(25,car_detail_log.getOaid());
            ps.setString(26,car_detail_log.getPubliclongitude());
            ps.setString(27,car_detail_log.getPubliclongitude());
            ps.setString(28,car_detail_log.getPubliccitycode());
            ps.setString(29,car_detail_log.getAppname());
            ps.setString(30,car_detail_log.getDevicename());
            ps.setString(31,car_detail_log.getPublictoken());
            ps.setString(32,car_detail_log.getAppchannelid());
            ps.setString(33,car_detail_log.getAndroidid());
            ps.setString(34,car_detail_log.getMemno());
            ps.setString(35,car_detail_log.getMeminfocache());
            ps.setString(36,car_detail_log.getReturncarlon());
            ps.setString(37,car_detail_log.getReturncarlat());
            ps.setString(38,car_detail_log.getReturncarflag());
            ps.setString(39,car_detail_log.getH5platform());
        }catch (Exception e){
            System.out.println("error:车辆详情页插入数据出错了");
        }
        // 执行写入
        ps.execute();
    }


}
